package widgets;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zx.disneyholidays.R;
import com.zx.disneyholidays.utils.BaseUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 * 原因：子项目化开发，未移动
 * 涉及类：MenuDialog
 */
public class MenuDialog extends Dialog {

    public static final int TYPE_NEW = 0;
    public static final int TYPE_WIFI = 1;

    abstract class MenuAdapter extends BaseAdapter {

        @Override
        public int getCount() {

            return mSelections.size();

        }

        @Override
        public Object getItem(int position) {

            return mSelections.get(position);

        }

        @Override
        public long getItemId(int arg0) {

            return 0;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_menu_dialog, null);
                holder = new ViewHolder();

                convertView.setTag(holder);
                holder.title = (TextView) convertView
                        .findViewById(R.id.group_item);
                holder.icon = (ImageView) convertView
                        .findViewById(R.id.new_feature);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.title.setText(mSelections.get(position));
            if (position == mPosition2showNewIcon) {
                holder.icon.setVisibility(View.VISIBLE);
            } else {
                holder.icon.setVisibility(View.GONE);
            }
            refreshExtra(mSelections.get(position), holder);
            return convertView;

        }

        public abstract void refreshExtra(String caption, ViewHolder viewHolder);

    }

    public class ViewHolder {
        public ImageView icon;
        public TextView title;
    }

    public interface ExtraCallback {
        public void execute(String caption, ViewHolder holder);
    }

    private Activity mContext;
    private ListView mListView;

    private List<String> mSelections;
    private MenuAdapter mAdapter;
    private String mTitle = "请选择需要的操作";
    private OnItemClickListener mListener;
    private ExtraCallback mCallback;
    private int mDrawableType = TYPE_NEW;
    private boolean isPad = false;
    private int mPosition2showNewIcon = -1;

    public MenuDialog(Activity context, int titlesId, boolean isPad,
                      OnItemClickListener listener) {
        this(context, getTitles(context, titlesId), isPad, listener, null);
    }

    public MenuDialog(Activity context, int titlesId,
                      OnItemClickListener listener) {
        this(context, getTitles(context, titlesId), false, listener, null);
    }

    public MenuDialog(Activity context, int titlesId, boolean isPad,
                      OnItemClickListener listener, ExtraCallback callback) {
        this(context, getTitles(context, titlesId), isPad, listener, callback);
    }

    public MenuDialog(Activity context, int titlesId,
                      OnItemClickListener listener, ExtraCallback callback) {
        this(context, getTitles(context, titlesId), false, listener, callback);
    }

    public MenuDialog(Activity context, List<String> titles, boolean isPad,
                      OnItemClickListener listener) {
        this(context, titles, isPad, listener, null);
    }

    public MenuDialog(Activity context, List<String> titles, boolean isPad,
                      OnItemClickListener listener, int newDrawable) {
        this(context, titles, isPad, listener, null);
        setDrawable(newDrawable);
    }

    public MenuDialog(Activity context, List<String> titles,
                      OnItemClickListener listener) {
        this(context, titles, false, listener, null);
    }

    public MenuDialog(Activity context, List<String> titles, boolean isPad) {
        this(context, titles, isPad, null, null);
    }

    public MenuDialog(Activity context, List<String> titles) {
        this(context, titles, false, null, null);
    }

    public MenuDialog(Activity context, int titlesId) {
        this(context, getTitles(context, titlesId), false, null, null);
    }

    public MenuDialog(Activity context, List<String> titles, boolean isPad,
                      OnItemClickListener listener, ExtraCallback callback) {
        super(context, R.style.menuDialog);
        this.mContext = context;
        this.mSelections = titles;
        this.mListener = listener;
        this.mCallback = callback;
    }

    private static List<String> getTitles(Context context, int resId) {
        List<String> target = new ArrayList<String>();
        CharSequence[] arrays = context.getApplicationContext().getResources()
                .getTextArray(resId);
        for (CharSequence a : arrays) {
            target.add(a.toString());
        }
        return target;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_menu_dialog);
        if (isPad) {
            LinearLayout ll = (LinearLayout) findViewById(R.id.context_ll);
            LayoutParams lp = ll.getLayoutParams();
            lp.width = BaseUtil.dp2px(getContext(), 350);
            ll.setLayoutParams(lp);
        }
        mListView = (ListView) findViewById(R.id.listview);
        ((TextView) findViewById(R.id.title_tv)).setText(mTitle);
        mAdapter = new MenuAdapter() {

            @Override
            public void refreshExtra(String caption, ViewHolder Holder) {
                if (mCallback != null) {
                    mCallback.execute(caption, Holder);
                }

            }
        };
        mListView.setAdapter(mAdapter);
    }

    public void setHeaderTitle(String title) {
        mTitle = title;
    }

    public void setSelections(List<String> selections) {
        this.mSelections = selections;
        if (null != mAdapter) mAdapter.notifyDataSetChanged();
    }

    public List<String> getSelections() {
        return mSelections;
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        mListener = l;
    }

    public void setSelection(int index, String selection) {
        if (mSelections == null || index < 0 || index > mSelections.size()) {
            return;
        }
        mSelections.set(index, selection);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    public void setDrawable(int drawable) {
        this.mDrawableType = drawable;
    }

    public void showListNewIcon(int position) {
        this.mPosition2showNewIcon = position;
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void show() {
        super.show();
        mListView.setOnItemClickListener(mListener);
    }

    public int getPositionShowNewIcon() {
        return mPosition2showNewIcon;
    }
}